
import { GetModelList, PostModel, DeleteModel } from '@/api/registerLog'
import { deepClone } from "@/utils"
import { GetAllOrgList } from "@/api/org"
import waves from "@/directive/waves" // Waves directive
import { GetDictionaryByPCode } from '@/api/dictionary'

const defaultModel = {
  Id: '',
  UserId: '',
  MobilePhone: '',
  OrgCode: ''
}
export default {
  directives: { waves },
  data() {
    return {
      viewModel: {
        page: 1,
        pageSize: 10,
        total: 0,
        MobilePhone: '',
        OrderBy: 'CreateTime',//排序字段
        SortDir: '' //是否升序
      },
      views: [],
      dialogVisible: false,
      dialogType: '',
      modelList:[],
      model: Object.assign({}, defaultModel),
      allOrg: [],//全部组织列表
      loading: true,
      rules: {
        OrgCode: [
          { required: true, message: "请选择学校", trigger: "blur" }
        ],
      },
    }
  },
  computed: { // 计算属性，只要相关联的值没有改变，直接返回，不再进行计算，与方法区别，缓存
    routesData() {
      return this.routes;
    }
  },
  created() {  // 实例创建完成后执行的代码
    this.query();
    this.getAllOrgList();
  },
  methods: {
    async getAllOrgList() {
      const res = await GetAllOrgList();
      this.typeOptions = deepClone(res.data.OrgItems);
      this.typeOptions.unshift({ org_code: '', org_name: '全部' })
      this.allOrg = res.data.OrgItems;
    },

    async getModelList(viewModel) {
      this.loading = true;
      var res = await GetModelList(viewModel);
      if (res.data) {
        this.viewModel.total = res.data.TotalCount;
        this.modelList = res.data.Items;
      }

      this.loading = false;
    },
    query() {
      this.viewModel.page = 1;
      this.getModelList(this.viewModel);
    },
    refresh() {
      this.viewModel.MobilePhone = '';

      this.viewModel.page = 1;
      this.getModelList(this.viewModel);
    },
    lastPage(val) {
      this.viewModel.page = val;
    },
    nextPage(val) {
      this.viewModel.page = val;
    },
    handleSizeChange(val) {
      this.viewModel.pageSize = val;
      this.getModelList(this.viewModel);
    },
    handleCurrentChange(val) {
      this.viewModel.page = val
      this.getModelList(this.viewModel)
    },
    handleEdit(scope) {
      this.dialogType = 'edit';
      this.dialogVisible = true;
      this.model = deepClone(scope.row);
      this.$nextTick(() => {
        this.$refs['model'].clearValidate() //清空上次的错误验证提示
      });
    },
    async confirm() {
      //如果把验证失败弹出提示代码放在validate里面则不会弹出错误提示，直接走下面的更新操作代码
      var v;
      this.$refs['model'].validate(valid => {
        v = valid;
      });
      if (!v) {
        return false;
      }
      var res = await PostModel(this.model)
      this.viewModel.page = 1
      if (res && res.code == 200) {
        this.getModelList(this.viewModel);
        this.dialogVisible = false;
        this.$notify({
          title: "成功",
          type: "success",
          message: "操作成功!"
        });
      }
      // else {
      //   this.$notify({
      //     title: "失败",
      //     type: "error",
      //     message: res.message
      //   });
      // }
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          var res = await DeleteModel(scope.row.Id);
          if (res && res.code == 200) {
            this.$notify({
              title: "成功",
              type: "success",
              message: "操作成功!"
            });
          } else {
            this.$notify({
              title: "失败",
              type: "error",
              message: res.message
            });
          }
          this.getModelList(this.viewModel);
        })
        .catch(err => {
          console.error(err);
        });
    },
  }
}